home *** CD-ROM | disk | FTP | other *** search
- { menubits.pas -- Create menu items from bitmaps }
-
- program MenuBits;
-
- {$R menubits.res}
-
- uses WinTypes, WinProcs, WObjects;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_Quit = 101; { Exit command ID }
- num_Items = 9; { Number of custom menu items }
-
- type
-
- MenuBitsApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- MenuItemRec = record
- Name: String[20]; { Text for menu item }
- BMap: HBitmap; { Handle to item's bitmap }
- CommandID: Integer { ID returned for command }
- end;
-
- PMenuBitsWindow = ^MenuBitsWindow;
- MenuBitsWindow = object(TWindow)
- MenuItems: array[0 .. num_Items - 1] of MenuItemRec;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- destructor Done;
- virtual;
- procedure InitMenuItems;
- procedure MakeBitmaps;
- procedure SetupWindow;
- virtual;
- procedure CMQuit(var Msg: TMessage);
- virtual cm_First + cm_Quit;
- end;
-
-
- { MenuBitsApplication }
-
- {- Initialize MenuBitsApplication object's window }
- procedure MenuBitsApplication.InitMainWindow;
- begin
- MainWindow := New(PMenuBitsWindow, Init(nil, 'MenuBits'))
- end;
-
-
- { MenuBitsWindow }
-
- {- Construct MenuBitsWindow object; load bitmap resources }
- constructor MenuBitsWindow.Init(AParent: PWindowsObject;
- ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- InitMenuItems
- end;
-
- {- Destroy MenuBitsWindow; delete bitmaps }
- destructor MenuBitsWindow.Done;
- var
- I: Integer;
- begin
- for I := 0 to num_Items - 1 do
- DeleteObject(MenuItems[I].Bmap);
- TWindow.Done
- end;
-
- {- Attach bitmaps to menu }
- procedure MenuBitsWindow.SetupWindow;
- var
- I: Integer;
- Mh: HMenu;
- begin
- MakeBitmaps;
- Mh := CreatePopupMenu;
- if Mh <> 0 then
- begin
- AppendMenu(Attr.Menu, mf_Popup, Mh, '&Bitmaps');
- for I := 0 to num_Items - 1 do with MenuItems[I] do
- AppendMenu(Mh, mf_Bitmap, CommandID, PChar(MakeLong(Bmap, 0)))
- end;
- DrawMenubar(HWindow)
- end;
-
- {- Initialize MenuItems array (bitmaps created later) }
- procedure MenuBitsWindow.InitMenuItems;
- var
- I: Integer;
- S: string[5];
- begin
- for I := 0 to num_Items - 1 do with MenuItems[I] do
- begin
- Str(I + 1, S);
- Name := 'Menu item #' + S;
- CommandID := 201 + I
- end
- end;
-
- {- Create menu-item bitmaps }
- procedure MenuBitsWindow.MakeBitmaps;
- var
- I: Integer;
- Dc, MemDC: HDC;
- OldBitmap: HBitmap;
- FillBrush: HBrush;
- R: TRect;
- MenuFont, OldFont: HFont;
- N: LongInt;
- begin
- Dc := GetDC(HWindow);
- MemDC := CreateCompatibleDC(Dc);
- FillBrush := CreateSolidBrush(GetSysColor(color_Menu));
- SetTextColor(MemDC, GetSysColor(color_MenuText));
- SetBkColor(MemDC, GetSysColor(color_Menu));
- MenuFont := GetStockObject(ansi_Var_Font);
- OldFont := SelectObject(MemDC, MenuFont);
- with R do
- begin
- Left := 0; Top := 0; Right := -1; Bottom := -1;
- for I := 0 to num_Items - 1 do with MenuItems[I] do
- begin
- N := GetTextExtent(MemDC, @Name[1], Length(Name));
- if LOWORD(N) > Right then Right := LOWORD(N);
- if HIWORD(N) > Bottom then Bottom := HIWORD(N)
- end;
- for I := 0 to num_Items - 1 do with MenuItems[I] do
- begin
- Bmap := CreateCompatibleBitmap(Dc, Right, Bottom);
- OldBitmap := SelectObject(MemDC, Bmap);
- FillRect(MemDC, R, FillBrush);
- TextOut(MemDC, 0, 0, @Name[1], Length(Name));
- Bmap := SelectObject(MemDC, OldBitmap)
- end;
- end;
- SelectObject(MemDC, OldFont);
- DeleteDC(MemDC);
- ReleaseDC(HWindow, Dc);
- DeleteObject(FillBrush)
- end;
-
- {- End program when Exit command is selected }
- procedure MenuBitsWindow.CMQuit(var Msg: TMessage);
- begin
- CloseWindow
- end;
-
- var
-
- MenuBitsApp: MenuBitsApplication;
-
- begin
- MenuBitsApp.Init('MenuBitsApp');
- MenuBitsApp.Run;
- MenuBitsApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 2/26/1991
- ---------------------------------------------------------------}
-